@page "/devices"
@using System.Diagnostics
@using BlazorReset.Resources
@using BlazorReset.Utils
@inject  NavigationManager Navigation
@inject SearchPageState State
@inject Bus Bus
@inject HomeState HomeState
@implements IDisposable

@if (_busy)
{
    <MudText
    Class="d-flex  align-center">
        <MudProgressCircular Style="width: 16px; height: 16px;" Indeterminate Class="mr-2" />
        @Strings.Searching
    </MudText>
}
else if (State.Devices is [])
{
    <MudText>
        @Strings.NoDeviceFound
    </MudText>
}
else
{
    <MudTable
    Class="mt-4"
    T="Device"
    FixedHeader
    Items="@State.Devices"
    Hover="true"
    Breakpoint="Breakpoint.Sm"
    RowClass="cursor-pointer"
    Dense
    OnRowClick="OpenInHome">
        <HeaderContent>
            <MudTh>
                <MudTableSortLabel T="Device" InitialDirection="SortDirection.Ascending" Enabled SortBy="arg => arg.Ip">
                    @Strings.IP
                </MudTableSortLabel>
            </MudTh>
            <MudTh>
                <MudTableSortLabel T="Device" Enabled SortBy="arg => arg.Platform">
                    @Strings.Model
                </MudTableSortLabel>
            </MudTh>
            <MudTh>
                <MudTableSortLabel T="Device" Enabled SortBy="arg => arg.System">
                    @Strings.Platform
                </MudTableSortLabel>
            </MudTh>
            <MudTh>
                @Strings.Operation
            </MudTh>
        </HeaderContent>
        <RowTemplate>
            <MudTd DataLabel="IP">@context.Ip</MudTd>
            <MudTd DataLabel="Model">@context.Platform</MudTd>
            <MudTd DataLabel="System">@context.System</MudTd>
            <MudTd DataLabel="Operations">
                <MudStack Row>
                    <MudTooltip Text="@Strings.OpenInBrowser">
                        <MudIconButton Icon="@Icons.Material.Filled.Launch" OnClick="@(() => OpenInBrowser(context))" />
                    </MudTooltip>
                    <MudTooltip Text="@Strings.OpenInTelnet">
                        <MudIconButton Icon="@Icons.Material.Filled.Terminal" OnClick="@(() => OpenTelnet(context.Ip))" />
                    </MudTooltip>
                </MudStack>
            </MudTd>
        </RowTemplate>
    </MudTable>
}
    <MudFab Color="Color.Primary"
    StartIcon="@Icons.Material.Filled.Refresh"
    Style="position: fixed; right: 16px; bottom: 16px"
    OnClick="@SearchDevicesAsync"
    />

@code {
    bool _busy = false;

    void OnLanguageChanged(object? sender, EventArgs e)
    {
        StateHasChanged();
    }

    protected override async Task OnInitializedAsync()
    {
        Bus.LanguageChanged += OnLanguageChanged;
        if (State.Devices == null)
        {
            await SearchDevicesAsync();
        }
	}


	private async Task SearchDevicesAsync()
	{
        _busy = true;
        State.Devices = await Utils.Helper.SearchDevicesAsync(10, CancellationToken.None);
        _busy = false;
	}

	void OpenInHome(TableRowClickEventArgs<Device> d)
	{
        HomeState.Ip = d.Item!.Ip;
        HomeState.DeviceType = d.Item!.Platform.PlatFormToDeviceType();
		Navigation.NavigateTo("/");
	}

	void OpenInBrowser(Device d)
	{
		var url = $"http://{d.Ip}";
		var si = new ProcessStartInfo(url);
		si.UseShellExecute = true;
		Process.Start(si);
	}

	void OpenTelnet(string ip)
	{
		var info = new ProcessStartInfo()
		{
			FileName = "telnet.exe",
			Arguments = ip,
			//RedirectStandardInput = true,
			UseShellExecute = true
		};

		var process = new Process{StartInfo = info};
		process.Start();

		/*var writer = process.StandardInput;
		writer.WriteLine("root");
		writer.WriteLine("antslq");*/

	}

    public void Dispose()
    {
        Bus.LanguageChanged -= OnLanguageChanged;
    }

}
